using System;
using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Altairis.FluffyCloud.WebCore.DynamicData.FieldTemplates {

    public partial class Text_EditField : System.Web.DynamicData.FieldTemplateUserControl {

        public override Control DataControl {
            get {
                return TextBox1;
            }
        }

        protected override void ExtractValues(IOrderedDictionary dictionary) {
            dictionary[Column.Name] = ConvertEditedValue(TextBox1.Text);
        }

        protected override void OnDataBinding(EventArgs e) {
            base.OnDataBinding(e);
            if (Column.MaxLength > 0) {
                TextBox1.MaxLength = Math.Max(FieldValueEditString.Length, Column.MaxLength);
            }
        }

        protected void Page_Load(object sender, EventArgs e) {
            if (Column.MaxLength < 20) {
                TextBox1.Columns = Column.MaxLength;
            }
            TextBox1.ToolTip = Column.Description;

            if (Column.DataTypeAttribute != null) {
                switch (Column.DataTypeAttribute.DataType) {
                    case DataType.EmailAddress:
                        TextBox1.TextMode = TextBoxMode.Email;
                        break;

                    case DataType.Password:
                        TextBox1.TextMode = TextBoxMode.Password;
                        break;

                    case DataType.PhoneNumber:
                        TextBox1.TextMode = TextBoxMode.Phone;
                        break;

                    case DataType.Url:
                        TextBox1.TextMode = TextBoxMode.Url;
                        break;
                }
            }

            SetUpValidator(RequiredFieldValidator1);
            SetUpValidator(RegularExpressionValidator1);
            SetUpValidator(DynamicValidator1);
        }
    }
}